package com.starsoft.quant.executor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.starsoft.quant.bean.QuantDetail;
import com.starsoft.quant.bean.QuantSummary;
import com.starsoft.trade.order.Order;

public class StrategyReport {
	
	private QuantSummary summary;
	
	private List<QuantDetail> details;
	private Map<String, Integer> detailMap = new HashMap<String, Integer>();
	
	private final List<Order> orders = new ArrayList<>();

	public QuantSummary getSummary() {
		return summary;
	}

	public void setSummary(QuantSummary summary) {
		this.summary = summary;
	}

	public List<QuantDetail> getDetails() {
		return details;
	}

	public void setDetails(List<QuantDetail> details) {
		Collections.sort(details, new Comparator<QuantDetail>() {
			public int compare(QuantDetail o1, QuantDetail o2) {
				return -o1.getRating().compareTo(o2.getRating());
			}
		});
		for(int i=0;i<details.size();i++){
			QuantDetail detail = details.get(i);
			detailMap.put(detail.getId().getSecId(), i);
		}
		this.details = details;
	}
	
	public QuantDetail getDetail(String secId) {
		if (detailMap.containsKey(secId)) {
			return details.get(detailMap.get(secId));
		} else {
			return null;
		}
	}
	
	public QuantDetail getDetail(Integer index){
		return details.get(index);
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders.clear();
		for (QuantDetail detail : details) {
			detail.setTrade(null);
		}
		for(Order order : orders){
			addOrder(order);
		}
	}

	public void addOrder(Order order) {
		if (order.getSecNum() != 0) {
			QuantDetail detail = getDetail(order.getSecId());
			double secNum = order.getSecNum();
			detail.setTrade((int)secNum);
			orders.add(order);
		}
	}
	
	public void setRunlog(String runLog){
		summary.setRunLog(runLog);
	}
	
}
